package cn.edu.ctbu.dp01.api;

import cn.edu.ctbu.dp01.constant.REnum;
import cn.edu.ctbu.dp01.entity.Student;
import cn.edu.ctbu.dp01.exception.RException;
import cn.edu.ctbu.dp01.service.StudentService;
import cn.edu.ctbu.dp01.util.RUtil;
import cn.edu.ctbu.dp01.vo.QueryObj;
import cn.edu.ctbu.dp01.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/student")
public class StudentRestAplController {

    @Autowired
    private StudentService studentService;
    @GetMapping("/list")
    public R<List<Student>> findAll() {
        List<Student> students = studentService.getAll();
        return RUtil.success(students);
    }
    @GetMapping("/{id}")
    public R<Student> findById(@PathVariable int id) {
        Student student = studentService.findById(id);
        return RUtil.success(student);
    }

    @PostMapping("/add")
    public R<Student> add(Student student)
    {
        return RUtil.success(studentService.add(student));
    }

   @PutMapping("/update")
    public R<Student> update(Student student) {
       System.out.println("student = " + student);
        return RUtil.success( studentService.update(student));
    }
    @DeleteMapping("/delete")
    public R delete(@RequestParam("ids") String ids) {
        String[] idArray = ids.split(",");
        for (String idStr : idArray) {
            int id = Integer.parseInt(idStr);
            studentService.delete(id);
        }
        return RUtil.success();
    }

    @GetMapping("/validateUser")
    public R validateSnoAndPassword(String sno,String password)throws Exception{
        return RUtil.success(studentService.validDateUsernameAndPassword(sno,password));
    }

    @PostMapping("/getbypage")
    public R<Page<Student>> getByPage(@RequestBody QueryObj<Student> qObj) {
        // 修改排序为升序
        Sort sort = Sort.by(Sort.Direction.ASC, "id");  // 改为升序排序

        Integer pageIndex = 0;
        Integer pageSize = 10;

        if (qObj == null) {
            // 默认分页
            Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
            Page<Student> students = studentService.getAll(pageable);
            return RUtil.success(students.getContent(), students.getTotalElements());
        } else {
            pageIndex = qObj.getPage() - 1;
            pageSize = qObj.getLimit();

            if (qObj.getData() instanceof Student) {
                Student student = (Student) qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);

                // 添加对 age 字段的匹配规则
                ExampleMatcher matcher = ExampleMatcher.matching()
                        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withMatcher("age", ExampleMatcher.GenericPropertyMatchers.exact()) // 精确匹配 age 字段
                        .withIgnoreNullValues();

                Example<Student> example = Example.of(student, matcher);
                Page<Student> studentPage = studentService.getAll(example, pageable);
                return RUtil.success(studentPage.getContent(), studentPage.getTotalElements());
            } else {
                throw new RException(REnum.QUERY_ERR);
            }
        }
    }

}

